package com.laijiale.mapper;

import com.laijiale.entities.EmpRole;
import com.laijiale.entities.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:51
 */
@Repository
public interface RoleMapper {
    @Insert("insert into ums_role (title,info,deleted,created,updated )" +
            "values(#{title},#{info},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Role role);


    @Select("select ur.id, ur.title,ur.info, ur.deleted, ur.created, ur.updated \n" +
            "from ums_role ur" +
            " where ur.id=#{param1} and ur.deleted=0")
    Role selectById(Long id);

    @Update("<script>" +
            "update ums_role" +
            "<set>" +
            "<if test='title !=null'>title=#{title},</if> " +
            "<if test='info !=null'>info=#{info},</if> " +

            "<if test='deleted !=null'>deleted=#{deleted},</if> " +

            "<if test='created !=null'>created=#{created},</if> " +

            "<if test='updated !=null'>updated=#{updated},</if> " +
            "</set> " +
            "where id =#{id}  and deleted =0" +
            "</script>")
    int updated(Role role);

    @Update("update ums_role set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);
    @Update("<script>" +
            "update ums_role set deleted =1,updated = now() where " +
            "<foreach collection = 'list' item='e' open ='id in (' close= ')' separator = ','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);
@Select("select  ur.id, ur.title,ur.info, ur.deleted, ur.created, ur.updated from ums_role  ur")
    List<Role> list();

    @Select("select ur.id, ur.title from ums_role ur " +
            "where ur.title = #{title} ")
     Role selectByAllTitle(Role role);

    @Delete("delete from ums_role where id =#{param1}")
    int deleteById(Long id);

    @Select("select ur.id, ur.title from ums_role ur " +
            "where ur.title = #{param1} ")
    Role selectByTitle(String title);
    @Select(" select ur.id, ur.title, ur.info, ur.deleted, ur.created, ur.updated " +
            "from ums_emp_role uer " +
            " join ums_role ur on uer.fk_role_id = ur.id " +
            " join ums_emp ue on uer.fk_emp_id = ue.id " +
            "where uer.fk_emp_id =${param1} and ur.deleted = 0 and ue.deleted = 0 " +
            "  and uer.deleted = 0 ")
    List<Role>   selectByEmpId(Long empID);

    @Update("update ums_emp_role set deleted = 1, updated = now() where id = #{EmpId}")
    int logicDeleteByEmpId(Long EmpId);

    @Insert("insert into ums_emp_role (fk_emp_id,fk_role_id,deleted,created,updated )" +
            "values(#{fkEmpId},#{fkRoleId},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertEmpRole(EmpRole empRole);

    @Select("select  ur.id, ur.title, ur.info,ur.deleted, " +
            "ur.created,ur.updated  from ums_role ur "+
            "where ur.deleted=0 and ur.title like concat('%',#{param1},'%') "
    )
    List<Role> listLikeTitle(String title);

}
